﻿ 
using SHKF.B2C.Model.Entity.Center;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text; 

namespace SHKF.B2C.BLL.Center
{
    public class ShopBLL: CenterBLL
    {
        public ShopBLL(SqlSugarClient db ) : base(db)
        {

        }

        public ShopEntity Get(long SN)
        {
            ShopEntity shopEntity = this.db
                .Queryable<ShopEntity>()
                .Where(oo => oo.SN == SN)
                .First();

            return shopEntity;
        }

        public List<ShopEntity> Query(long shopSN)
        {
            List<ShopEntity> list = this.db
                .Queryable<ShopEntity>()
                .Where(oo => oo.ShopSN == shopSN)
                .ToList();
            return list;
        }

        //public List<ShopEntity> Query(string platform, string shopName)
        //{ 
        //    List<ShopEntity> list = this.freeSql
        //        .Select<ShopEntity>()
        //        .Where(oo => oo.Platform == platform && oo.ShopName.Contains(shopName))
        //        .ToList();

        //    return list;
        //}

        public List<ShopEntity> Query(int CompanySN, string shopName)
        {
            var cmd = this.db
                .Queryable<ShopEntity>();


            if (CompanySN > 0)
            {
                cmd.Where(oo => oo.CompanySN == CompanySN);
            }
             
            if(!string.IsNullOrEmpty(shopName))
            {
                cmd.Where(oo => oo.ShopName.Contains(shopName));
            }

            List<ShopEntity> list = cmd
                .ToList();
            return list;
        }

        public List<ShopEntity> Query(int CompanySN, long ShopSN, string shopName)
        {
            var cmd = this.db
                .Queryable<ShopEntity>();

            if (CompanySN > 0)
            {
                cmd.Where(oo => oo.CompanySN == CompanySN);
            }

            if (ShopSN > 0)
            {
                cmd.Where(oo => oo.ShopSN == ShopSN);
            }

            List<ShopEntity> list = cmd
                .Where(oo => oo.ShopName.Contains(shopName))
                .ToList();
            return list;
        }

        public ShopEntity Save(ShopEntity shop)
        { 
            long SN = 0; 
            if (shop.SN == 0)
            {
                SN = this.db
                    .Insertable<ShopEntity>(shop)
                    .ExecuteReturnBigIdentity();

                shop.SN = SN;

                //创建门店的系统管理员, 密码是111111

            }
            else
            {
               this.db
                    .Updateable<ShopEntity>(shop) 
                    .ExecuteCommand();
            }
             
            return shop;
        }

        public void Delete(long SN)
        {
            int result = this.db
                .Deleteable<ShopEntity>()
                .In(SN) 
                .ExecuteCommand();
        }

    }
}
